package com.lysj.admin.master.auth.controller;

import cn.hutool.core.util.ArrayUtil;
import com.lysj.admin.master.auth.domain.Permission;
import com.lysj.admin.master.auth.domain.User;
import com.lysj.admin.master.auth.mapper.PermissionMapper;
import com.lysj.admin.master.auth.service.PermissionService;
import com.lysj.admin.master.auth.service.UserService;
import com.lysj.admin.master.auth.vo.PermissionVO;
import com.lysj.admin.master.common.CommonConstant;
import com.lysj.admin.master.common.annotation.Delete;
import com.lysj.admin.master.common.annotation.Save;
import com.lysj.admin.master.common.annotation.TokenInfo;
import com.lysj.admin.master.common.annotation.Update;
import com.lysj.admin.master.common.cache.RedisKeyEnum;
import com.lysj.admin.master.common.cache.RedisUtil;
import com.lysj.admin.master.common.spring.base.BaseController;
import com.lysj.admin.master.common.spring.base.BaseService;
import com.lysj.admin.master.common.spring.mpbase.MyController;
import com.lysj.admin.master.common.spring.mpbase.MyService;
import com.lysj.admin.master.common.spring.pagination.PageRequestDTO;
import com.lysj.admin.master.common.web.ParamUtil;
import com.lysj.admin.master.common.web.Resp;
import com.lysj.admin.master.common.web.RespUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author Created by zk on 2019-12-05 3:40
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/auth/permission")
public class PermissionController extends MyController<PermissionMapper,Permission, PageRequestDTO> {
    @Resource
    private PermissionService permissionService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private UserService userService;

    @Override
    public MyService<PermissionMapper, Permission> getService() {
        return permissionService;
    }

    @Save
    @Override
    public Resp save(Permission entity) {
        return super.save(entity);
    }


    @GetMapping("/findMenuList")
    public Resp findMenuList(@TokenInfo String userId) {
        User user = redisUtil.get(RedisKeyEnum.USER, userId, () -> userService.getById(userId));
        Optional<List<PermissionVO>> userPermissionsOp = redisUtil.get(RedisKeyEnum.ROLE_PERMISSION, user.getRoleId());
        List<PermissionVO> userPermissions = userPermissionsOp.orElse(new ArrayList<>());
        if (ParamUtil.isBlank(userPermissions)) {
            userPermissions = permissionService.findMenuList(user.getRoleId());
            if (!ParamUtil.isBlank(userPermissions)) {
                redisUtil.set(RedisKeyEnum.ROLE_PERMISSION, user.getRoleId(), userPermissions);
            }
        }
        return RespUtil.success(userPermissions);
    }

    @GetMapping("/find_all_list")
    public Resp findAllList(String type) {
        List<Permission> permissions = permissionService.findAllList(type);
        return RespUtil.success(permissions);
    }

    @Override
    @Update
    public Resp update(Permission entity) {
        permissionService.updatePermission(entity);
        return RespUtil.success("修改成功");
    }

    @Override
    @Delete
    public Resp delete(String[] ids) {
        permissionService.deletePermission(ids);
        return RespUtil.success("删除成功");
    }
}
